package com.ma.implatform.controller;

import com.ma.implatform.dto.GroupDndDTO;
import com.ma.implatform.dto.GroupInviteDTO;
import com.ma.implatform.dto.GroupMemberRemoveDTO;
import com.ma.implatform.dto.GroupMessageDTO;
import com.ma.implatform.enums.MessageType;
import com.ma.implatform.result.Result;
import com.ma.implatform.result.ResultUtils;
import com.ma.implatform.service.GroupService;
import com.ma.implatform.vo.GroupMemberVO;
import com.ma.implatform.vo.GroupMessageVO;
import com.ma.implatform.vo.GroupVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "群聊")
@RestController
@RequestMapping("/group")
@RequiredArgsConstructor
public class GroupController {
    private final GroupService groupService;
    @PostMapping("/create")
    @Operation(summary = "创建群聊",description = "创建群聊")
    public Result<GroupVO> createGroup(@Valid @RequestBody GroupVO vo){
        return ResultUtils.success(groupService.createGroup(vo));
    }
    @Operation(summary = "修改群聊消息",description = "修改群聊消息")
    @PutMapping("/modify")
    public Result<GroupVO> modifyGroup(@Valid @RequestBody GroupVO vo){
        return ResultUtils.success(groupService.modifyGroup(vo));
    }
    @Operation(summary = "解散群聊",description = "解散群聊")
    @DeleteMapping("/delete/{groupId}")
    public Result deleteGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId){
        groupService.deleteGroup(groupId);
        return ResultUtils.success();
    }
    @Operation(summary = "查询群聊",description = "查询单个群聊信息")
    @GetMapping("/find/{groupId}")
    public Result<GroupVO> findGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId){
        return ResultUtils.success(groupService.findById(groupId));

    }
    @Operation(summary = "查询群里列表",description = "查询群里列表")
    @GetMapping("/list")
    public Result<List<GroupVO>> findGroups() {
        return ResultUtils.success(groupService.findGroups());
    }
    @Operation(summary = "邀请进群",description = "邀请好友进群")
    @PostMapping("/invite")
    public Result invite(@Valid @RequestBody GroupInviteDTO dto){
        groupService.invite(dto);
        return ResultUtils.success();
    }
    @Operation(summary = "查询群里成员",description = "查询群里成员")
    @PostMapping("/members/{groupId}")
    public Result<List<GroupMemberVO>> findGroupMembers(@NotNull(message = "群里id不能为空") @PathVariable Long groupId){
        return ResultUtils.success(groupService.findGroupMembers(groupId));
    }
    @Operation(summary = "将成员移出群聊",description = "将成员移出群聊")
    @DeleteMapping("/members/remove")
    public Result removeMembers(@Valid @RequestBody GroupMemberRemoveDTO dto){
        groupService.removeGroupMembers(dto);
        return ResultUtils.success();
    }
    @Operation(summary = "退出群聊",description = "退出群聊")
    @DeleteMapping("/quit/{groupId}")
    public Result quitGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId){
        groupService.quitGroup(groupId);
        return ResultUtils.success();
    }
    @Operation(summary = "踢出群聊",description = "将用户踢出群聊")
    @DeleteMapping("/kick/{groupId}")
    public Result kictGroup(@NotNull(message = "群聊id不能为空")@PathVariable Long groupId,@NotNull(message = "用户id不能为空") @RequestParam Long userId){
        groupService.kickGroup(groupId,userId);
        return ResultUtils.success();
    }
    @Operation(summary = "开启/关闭免打扰",description = "开启/关闭免打扰")
    @PutMapping("/dnd")
    public Result setGroupDnd(@Valid @RequestBody GroupDndDTO dto){
        groupService.setDnd(dto);
        return ResultUtils.success();
    }
}
